package br.ufmg.ubicomp.communication;

import java.util.ArrayList;
import java.util.Collection;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import br.ufmg.ubicomp.utils.EntityProperties;

public class ServiceCommunicationService extends CommunicationService{

	public static final String BASE_URL = ConfigurationManager.getInstance().getData("server")+ 
		"service/process?operation=";

	/**
	 * lists all services in the system
	 * @return list of services.
	 * @throws Exception
	 */
	public Collection<EntityProperties> getListOfServices() throws Exception {
		String url = BASE_URL + "list";

		NodeList entities = processRequest(url);

		return getListOfServicesFromResult(entities);
	}

	private Collection<EntityProperties> getListOfServicesFromResult(
			NodeList entities) {
		
		Collection<EntityProperties> services = new ArrayList<EntityProperties>();

		for (int i = 0; i < entities.getLength(); i++) {
			Node entityEntry = entities.item(i);

			if (entityEntry instanceof Element) {
				EntityProperties service = getEntityElement(entityEntry);
				services.add(service);
			}
		}

		return services;
	}

	/**
	 * subscribes one user to a service in the system.
	 * @param serviceKey
	 * @param droidGuideUserKey
	 * @return
	 * @throws Exception
	 */
	public Collection<EntityProperties> subscribeToService(String serviceKey,
			String droidGuideUserKey) throws Exception {
		String url = BASE_URL + "subscribe&service_key=" + serviceKey
				+ "&droidGuideUser_key=" + droidGuideUserKey;

		NodeList entities = processRequest(url);

		Collection<EntityProperties> userToServices = new ArrayList<EntityProperties>();
		
		for (int i = 0; i < entities.getLength(); i++) {
			Node entityEntry = entities.item(i);

			if (entityEntry instanceof Element) {
				EntityProperties userToService = getEntityElement(entityEntry);
				userToServices.add(userToService);
			}
		}

		return userToServices;

	}

	/**
	 * unsubscribes an user to a service
	 * @param serviceKey
	 * @param droidGuideUserKey
	 * @return
	 * @throws Exception
	 */
	public Collection<EntityProperties> unsubscribeToService(String serviceKey,
			String droidGuideUserKey) throws Exception {
		String url = BASE_URL + "unsubscribe&service_key=" + serviceKey
				+ "&droidGuideUser_key=" + droidGuideUserKey;

		NodeList entities = processRequest(url); 

		Collection<EntityProperties> userToServices = new ArrayList<EntityProperties>();

		for (int i = 0; i < entities.getLength(); i++) {
			Node entityEntry = entities.item(i);

			if (entityEntry instanceof Element) {
				EntityProperties userToService = getEntityElement(entityEntry);
				userToServices.add(userToService);
			}
		}

		return userToServices;

	}

	/**
	 * gets the list of subscribed services
	 * @param droidGuideUserKey
	 * @return
	 * @throws Exception
	 */
	public Collection<EntityProperties> getSubscribedServices(String droidGuideUserKey) throws Exception {
		String url = BASE_URL + "listSubscribed&droidGuideUser_key=" + droidGuideUserKey;

		NodeList entities = processRequest(url); 
		
		return getListOfServicesFromResult(entities);

	}

}
